var util = require('../../../utils/util.js')
import Toast from '../../../dist/toast/toast';
import Dialog from '../../../dist/dialog/dialog';
var app = getApp();
Page({
  /**
   * 页面的初始数据
   */
  data: {
    avatarUrl:'',
    nickName:'',
    date:'',
    maxIndex:999,
    show: false,
    isSaveShow: true,
    accountContentData:[],
    allDataList:[],
    columns: []
  },
  onRemindTypeClose(){
    this.setData({
      show: false ,
      isSaveShow:true
    });
  },
  accountMark(event){
    var currentId=event.target.dataset.id;
    var currentindex=event.target.dataset.index;
    this.data.accountContentData.forEach((item,index)=>{
      if(item.id==currentId &&index==currentindex){
        item.accountMark=event.detail
      }
    });
  },
  money(event){
    var currentId=event.target.dataset.id;
    var currentindex=event.target.dataset.index;
    this.data.accountContentData.forEach((item,index)=>{
      if(item.id==currentId &&index==currentindex){
        item.money=event.detail
      }
    });
  },
  onChange(event) {
    const { picker, value, index } = event.detail;
    picker.setColumnValues(1, this.data.citys[value[0]]);
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

    var dateT = new Date();
//获取年份  
    var Y =dateT.getFullYear();
//获取月份  
    var M = (dateT.getMonth() + 1 < 10 ? '0' + (dateT.getMonth() + 1) : dateT.getMonth() + 1);
//获取当日日期 
    var D = dateT.getDate() < 10 ? '0' + dateT.getDate() : dateT.getDate();
    var dataString= Y + '-'  + M+ '-' + D;
    let _this = this;
    var avatarUrl = options.avatarUrl;
    var nickName = options.nickName;
    _this.setData({
      avatarUrl:avatarUrl,
      nickName:nickName,
      date:dataString
  });
  },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },
  bindDateChange: function (e) {
    this.setData({
      date: e.detail.value
    })
  },
  //保存账单
  saveAccount(){
    if(this.data.accountContentData.length==0){
      Toast("亲，你还没有记账哦！");
    }else{
      var dataUrl = app.globalData.apiBase +
        "/blog/addAccount";
      util.ajax(dataUrl, "post", {
        accountDetailDtoArray:JSON.stringify(this.data.accountContentData)
      }, (respResult,respCode)=>{
        if (respCode === '0000') {
          var pages = getCurrentPages();
          var prevPage = pages[pages.length - 2]; //上一个页面
          //直接调用上一个页面对象的setData()方法，把数据存到上一个页面中去
          prevPage.setData({
            isEmpty:false
          });
          wx.navigateBack({
            delta: 1
          })
        }else{
          Toast("保存失败了，联系老何");
        }
      });
    }
    console.log("保存")
  },
  onConfirmAccountType(event){
    const { picker, value, index } = event.detail;
    var labelVal=(`${value}`.split(','))[1];
    var selectFirst=(`${index}`.split(','))[0];
    if(this.data.allDataList[selectFirst].sonClassifyList.length==0){ //没有子账单
      Toast("请选择子账单");
      return false;
    }
    var selectSecond=(`${index}`.split(','))[1];
    console.log(this.data.allDataList[selectFirst].sonClassifyList[selectSecond].id+"+++")
    this.data.accountContentData.push({
      id:this.data.allDataList[selectFirst].sonClassifyList[selectSecond].id,
      label:labelVal,
      accountType:this.data.allDataList[selectFirst].sonClassifyList[selectSecond].accountType,
      classifyName:this.data.allDataList[selectFirst].sonClassifyList[selectSecond].classifyName,
    });
    this.setData({
      show:false,
      isSaveShow:true,
      accountContentData:this.data.accountContentData
    })
  },
  //删除指定的账单条目00
  onClickIcon(e){
    var currentId=e.target.dataset.id;
    console.log("currentId="+currentId)
    var currentIndex=e.target.dataset.index;
    Dialog.confirm({
      title: '老何提示',
      message: '确定删除这条记录了给',
      zIndex:999
    }).then(() => {
      var array=this.data.accountContentData;
      console.log(this.data.accountContentData);
      this.data.accountContentData.forEach((it,index)=>{
        if(index==currentIndex){
          array.splice(index,1)
        }
      });
      this.setData({
        accountContentData:array
      })
    })
  },

  //选择账单明细（下拉框）
  addAccountDetail(){
    this.setData({
      columns:{},
    });
      //获取该账户下所有的子账单分类
    var dataUrl = app.globalData.apiBase +
        "/blog/getAccountClassifyList";
    util.ajax(dataUrl, "post", {
    }, (respResult)=>{
      var data = {};
      respResult.forEach((item)=>{
          var subData=[];
          item.sonClassifyList.forEach((it)=>{
            subData.push(it.classifyName);
          });
          var variable = item.classifyName;
          data[variable]=subData
      });
      this.setData({
        citys:data,
        allDataList:respResult
      });
      const  columns=[
        {
          values: Object.keys(this.data.citys),
          className: 'column1'
        },
        {
          values: this.data.citys[respResult[0].classifyName],
          className: 'column2',
          defaultIndex: 2
        },
      ];
      this.setData({
        columns:columns
      })
    });
    this.setData({
      show:true,
      isSaveShow:false
    })
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },
  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

});
